//
//  HomeViewController.swift
//  VVbo
//
//  Created by Mr.Run on 2017/8/21.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit

private let originalcellID = "originalcellID"
private let retweetedcellID = "retweetedcellID"

class HomeViewController: BaseController {
    
    fileprivate lazy var listViewModel = StatusListViewModel()
    
    override func loadData() {
        self.refreshControl?.beginRefreshing()
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            if shouldRefresh{
                self.tableView?.reloadData()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavTitle()
        
        NotificationCenter.default.addObserver(self, selector: #selector(browserPhotos), name: NSNotification.Name(rawValue: StatusCellBrowserPhotoNotification), object: nil)
        
//        let m = EmoticonManager.shared
        
//        print(m.packages.last?.emoticons.first?.image)
        
//        print(m.findEmoticon(string: "[马上有对象]"))
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc fileprivate func browserPhotos(n:Notification) {
        guard let selectedIndex = n.userInfo?[StatusCellBrowserPhotoSelectedIndexKey] as? Int,
            let urls = n.userInfo?[StatusCellBrowserPhotoURLsKey] as? [String],
            let imageViewList = n.userInfo?[StatusCellBrowserPhotoImageViewsKey] as? [UIImageView]
            else {
                return
        }
        
        let vc = HMPhotoBrowserController.photoBrowser(withSelectedIndex: selectedIndex, urls: urls, parentImageViews: imageViewList)
        present(vc, animated: true, completion: nil)
        
    }
    
    
    @objc fileprivate func friends() {
//        print("aaa")
    }
    
}

extension HomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // FIXME: cellID
        
        let vm = listViewModel.statusList[indexPath.row]
        
        let cellID = (vm.status.retweeted_status != nil) ? retweetedcellID:originalcellID
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! StatusCell
        
//        cell.statusLabel?.text = (listViewModel.statusList[indexPath.row] as StatusViewModel).status.text
        cell.viewModel = vm
        cell.delegate = self
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let vm = listViewModel.statusList[indexPath.row]
        
        return vm.rowHeigth
    }
    
}

extension HomeViewController: StatusCellDelegate {
    
    func statusCellDidSelectedLinkStr(cell: StatusCell, urlString: String) {
        print(urlString)
        
        let vc = WebViewController()
        vc.urlString = urlString
        
        navigationController?.pushViewController(vc, animated: true)
        
    }
    
}

extension HomeViewController {
    override func setupTableView() {
        super.setupTableView()
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(friends))
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        tableView?.register(UINib(nibName: "StatusNormalCell", bundle: nil), forCellReuseIdentifier: originalcellID)
        tableView?.register(UINib(nibName: "StatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedcellID)
//        tableView?.rowHeight = UITableViewAutomaticDimension
//        tableView?.estimatedRowHeight = 300
        tableView?.separatorStyle = .none
    }
    
    fileprivate func setupNavTitle() {
        let title = NetworkManager.shared().userAccount.screen_name
        let button = TitleButton(title: title)
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        navItem.titleView = button
    }
    
    @objc private func clickTitleButton(btn:UIButton) {
        btn.isSelected = !btn.isSelected
        
    }
    
}
